package org.nf.web.servlet.adapter;

import cn.hutool.core.util.ReflectUtil;
import org.nf.web.servlet.Controller;
import org.nf.web.servlet.HandlerAdapter;
import org.nf.web.servlet.BaseView;
import org.nf.web.servlet.ServletApi;

/**
 * @author wangliang
 * @date 2023/4/19
 * 负责调用简单的Controller对象
 */
public class SimpleControllerHandlerAdapter implements HandlerAdapter {

    @Override
    public boolean supports(Object handler) {
        if(handler instanceof Class<?> handlerType) {
            return Controller.class.isAssignableFrom(handlerType);
        }
        return false;
    }

    @Override
    public BaseView handle(Object handler, ServletApi servletApi) throws Exception{
        Class<?> handlerType = (Class<?>) handler;
        Controller controller = (Controller) ReflectUtil.newInstance(handlerType);
        return controller.execute(servletApi.request(), servletApi.response());
    }
}
